{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "from __future__ import print_function, absolute_import\n",
    "from gm.api import *\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 可以直接提取数据，掘金终端需要打开，接口取数是通过网络请求的方式，效率一般，行情数据可通过subscribe订阅方式\n",
    "# 设置token， 查看已有token ID,在用户-密钥管理里获取\n",
    "set_token('bf739ec28ad507f391a3c3e99ed0365007da88f8')\n",
    "# set_token('ca9c4532786a122aa5ceb3fd726432862e046f91') # 135账户\n",
    "# data = stk_get_index_constituents(index='SHSE.000300')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 沪深300成分股票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=get_history_constituents(index='SHSE.000300', start_date='2024-05-01', end_date='2024-7-101')[0]['constituents'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['SHSE.601225', 'SZSE.300059', 'SZSE.000776', 'SHSE.603986', 'SHSE.600926', 'SZSE.002050', 'SZSE.000166', 'SHSE.600585', 'SZSE.000877', 'SZSE.002460', 'SZSE.300450', 'SHSE.600132', 'SHSE.600332', 'SHSE.601916', 'SHSE.601788', 'SHSE.600989', 'SZSE.000768', 'SHSE.600372', 'SHSE.600584', 'SHSE.688561', 'SHSE.601808', 'SZSE.002555', 'SHSE.601901', 'SHSE.600938', 'SHSE.600219', 'SHSE.688036', 'SZSE.300347', 'SHSE.600406', 'SHSE.600030', 'SZSE.000425', 'SZSE.002304', 'SHSE.600025', 'SHSE.601021', 'SHSE.688599', 'SHSE.600011', 'SHSE.600886', 'SHSE.603486', 'SZSE.300979', 'SZSE.002812', 'SHSE.601100', 'SZSE.002493', 'SHSE.688126', 'SZSE.300124', 'SHSE.601881', 'SHSE.603195', 'SHSE.600089', 'SHSE.603833', 'SZSE.002074', 'SZSE.000792', 'SZSE.002459', 'SZSE.000858', 'SHSE.600893', 'SHSE.601818', 'SHSE.601628', 'SZSE.300142', 'SHSE.688981', 'SZSE.002841', 'SHSE.600570', 'SHSE.603899', 'SZSE.002916', 'SZSE.300763', 'SHSE.600111', 'SHSE.603260', 'SHSE.600460', 'SHSE.601006', 'SHSE.601377', 'SHSE.600919', 'SHSE.600183', 'SZSE.000625', 'SZSE.000301', 'SHSE.601919', 'SHSE.601877', 'SHSE.600900', 'SZSE.002001', 'SHSE.600050', 'SHSE.688256', 'SHSE.601865', 'SHSE.688303', 'SHSE.601155', 'SHSE.603659', 'SHSE.600031', 'SHSE.600036', 'SHSE.601800', 'SHSE.600009', 'SHSE.600745', 'SHSE.600803', 'SZSE.002594', 'SZSE.300759', 'SHSE.600039', 'SHSE.601229', 'SHSE.603392', 'SHSE.688065', 'SZSE.000708', 'SZSE.002371', 'SZSE.000800', 'SHSE.601601', 'SZSE.002821', 'SHSE.688012', 'SZSE.300628', 'SHSE.601360', 'SHSE.600048', 'SZSE.002938', 'SZSE.300750', 'SZSE.000338', 'SZSE.000651', 'SHSE.688111', 'SZSE.300316', 'SZSE.300896', 'SHSE.600176', 'SZSE.000333', 'SHSE.600918', 'SZSE.002466', 'SHSE.601888', 'SHSE.601066', 'SHSE.600660', 'SHSE.601600', 'SZSE.000661', 'SZSE.001289', 'SHSE.603501', 'SHSE.601995', 'SZSE.002603', 'SZSE.001979', 'SHSE.601868', 'SHSE.601211', 'SHSE.600000', 'SZSE.000157', 'SHSE.600276', 'SZSE.000069', 'SHSE.601336', 'SHSE.600438', 'SHSE.600606', 'SZSE.300274', 'SHSE.601985', 'SHSE.600085', 'SZSE.002714', 'SHSE.601398', 'SHSE.600019', 'SZSE.000596', 'SHSE.601689', 'SZSE.300957', 'SZSE.000538', 'SHSE.601766', 'SZSE.003816', 'SHSE.688363', 'SHSE.600061', 'SHSE.600519', 'SHSE.601288', 'SHSE.600837', 'SHSE.601618', 'SZSE.000895', 'SZSE.000938', 'SHSE.603806', 'SHSE.601169', 'SZSE.301269', 'SZSE.300760', 'SZSE.300751', 'SHSE.603288', 'SZSE.300496', 'SZSE.000786', 'SZSE.002027', 'SHSE.603259', 'SHSE.601878', 'SZSE.300454', 'SZSE.002475', 'SHSE.601688', 'SHSE.601668', 'SHSE.601816', 'SHSE.600547', 'SZSE.300308', 'SHSE.600845', 'SHSE.601658', 'SZSE.300919', 'SZSE.300408', 'SZSE.002241', 'SHSE.601318', 'SZSE.300223', 'SZSE.002252', 'SHSE.601857', 'SZSE.002415', 'SHSE.600754', 'SZSE.002142', 'SHSE.688271', 'SHSE.601238', 'SZSE.000063', 'SHSE.601166', 'SHSE.601088', 'SZSE.300433', 'SHSE.600436', 'SZSE.002180', 'SHSE.601236', 'SHSE.603799', 'SZSE.000999', 'SHSE.600515', 'SHSE.600015', 'SZSE.002648', 'SZSE.002352', 'SHSE.605117', 'SHSE.601615', 'SHSE.688396', 'SHSE.603993', 'SHSE.603369', 'SZSE.000408', 'SHSE.601390', 'SHSE.601607', 'SHSE.601633', 'SZSE.000983', 'SHSE.601669', 'SHSE.601872', 'SZSE.300033', 'SHSE.601898', 'SHSE.600732', 'SHSE.600115', 'SHSE.601699', 'SZSE.002311', 'SHSE.601009', 'SHSE.600875', 'SZSE.300015', 'SHSE.601012', 'SHSE.600941', 'SZSE.002271', 'SZSE.002920', 'SZSE.300661', 'SHSE.601319', 'SZSE.300782', 'SHSE.601186', 'SHSE.688187', 'SHSE.601899', 'SHSE.601998', 'SHSE.601989', 'SHSE.600188', 'SHSE.601728', 'SHSE.601138', 'SHSE.688223', 'SHSE.600196', 'SZSE.002179', 'SHSE.600346', 'SZSE.000002', 'SZSE.002601', 'SZSE.000963', 'SHSE.600600', 'SHSE.601111', 'SHSE.600018', 'SHSE.600741', 'SHSE.600795', 'SZSE.300122', 'SHSE.605499', 'SHSE.600150', 'SHSE.603290', 'SHSE.601059', 'SZSE.002736', 'SHSE.600958', 'SHSE.600029', 'SHSE.600426', 'SHSE.688041', 'SHSE.600016', 'SZSE.300498', 'SHSE.600309', 'SHSE.601698', 'SHSE.600489', 'SHSE.601838', 'SZSE.000617', 'SHSE.600809', 'SHSE.600362', 'SZSE.000568', 'SZSE.002709', 'SHSE.600999', 'SHSE.600233', 'SZSE.000876', 'SZSE.300014', 'SHSE.601328', 'SZSE.002202', 'SZSE.002236', 'SHSE.600588', 'SHSE.600905', 'SZSE.002129', 'SHSE.600690', 'SZSE.300413', 'SHSE.600674', 'SZSE.300999', 'SZSE.002007', 'SHSE.603019', 'SZSE.000725', 'SZSE.002230', 'SHSE.601988', 'SZSE.002410', 'SHSE.601117', 'SHSE.600104', 'SHSE.688008', 'SHSE.601799', 'SZSE.000977', 'SZSE.000733', 'SHSE.600010', 'SZSE.000100', 'SHSE.600028', 'SHSE.600023', 'SZSE.000001', 'SHSE.601939', 'SHSE.600760', 'SHSE.600887', 'SZSE.002049'])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     open   high    low  close                       eob\n",
      "19  5.287  5.287  5.261  5.284 2024-05-31 00:00:00+08:00\n"
     ]
    }
   ],
   "source": [
    "# 查询历史行情, 采用定点复权的方式， adjust指定前复权，adjust_end_time指定复权时间点\n",
    "data1 = history(symbol='SZSE.159934', frequency='1d', start_time='2024-05-01 09:00:00', end_time='2024-05-31 16:00:00',\n",
    "               fields='eob,open,high,low,close', adjust=ADJUST_PREV, adjust_end_time='2024-05-31', df=True)\n",
    "\n",
    "# data['date'] = data['eob'].dt.strftime('%Y-%m-%d')\n",
    "# data1.insert(0, 'date', data1['eob'].dt.strftime('%Y-%m-%d'))\n",
    "print(data1.tail(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = history(symbol='SHSE.600000', frequency='1d', start_time='2020-01-01 09:00:00', end_time='2020-12-31 16:00:00',\n",
    "               adjust=ADJUST_PREV, adjust_end_time='2020-12-31', df=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fundamental= get_fundamentals(table='trading_derivative_indicator', symbols='SHSE.600000', start_date='2020-01-01 09:00:00', end_date='2020-01-01 09:00:00', fields='TOTMKTCAP', limit=40000, df=True).sort_values('TOTMKTCAP')\n",
    "fundamental"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_9676\\1859381516.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mprevious_N_trading_date\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mstart_date\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_previous_N_trading_date\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m2024\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcounts\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_9676\\1859381516.py\u001b[0m in \u001b[0;36mget_previous_N_trading_date\u001b[1;34m(date, counts, exchange)\u001b[0m\n\u001b[0;32m      6\u001b[0m     \"\"\"\n\u001b[0;32m      7\u001b[0m     \u001b[0mdate\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTimestamp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdate\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m     \u001b[0mprevious_N_trading_date\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_trading_dates\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexchange\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mexchange\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstart_date\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdate\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimedelta\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdays\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmax\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcounts\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcounts\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend_date\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdate\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mcounts\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mprevious_N_trading_date\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "def get_previous_N_trading_date(date,counts=1,exchange='SHSE'):\n",
    "    \"\"\"\n",
    "    获取end_date前N个交易日,end_date为datetime格式，不包括date日期\n",
    "    :param date：目标日期\n",
    "    :param counts：历史回溯天数，默认为1，即前一天\n",
    "    \"\"\"\n",
    "    date = pd.Timestamp(date)\n",
    "    previous_N_trading_date = get_trading_dates(exchange=exchange, start_date=date-datetime.timedelta(days=max(counts+30,counts*3)), end_date=date)[-counts-1]\n",
    "    return previous_N_trading_date\n",
    "\n",
    "start_date = get_previous_N_trading_date (2024-4-20,counts=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "current_data = current(symbols='SZSE.000001')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "current_data"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
